package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"reflect"
	"time"
	"gopkg.in/go-playground/validator.v8"
)

type Booking struct {
	CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
	CheckOut time.Time `form:"check_out" time_format:"2006-01-02" binding
						:"required,gtfield=CheckIn"`
}

func main(){
	r := gin.Default()
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("bookabledate", bookableDate)
	}
	r.GET("/bookable", testing)
	r.POST("/bookable", testing)
	r.Run()
}

func testing (c *gin.Context) {
	var b Booking
	if err:=c.ShouldBind(&b); err != nil {
		c.JSON(200, gin.H{"error": err})
		return
	}
	c.JSON(200, gin.H{
		"message": "ok",
		"booking": b,
	})

}

func bookableDate(
	v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
	field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
	if date, ok := field.Interface().(time.Time); ok {
		today := time.Now()
		if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
			return true
		}
	}
	return false
}
